#include "stdafx.h"
#include "Button.h"
#include "SkCanvas.h"
#include "WidgetWin.h"

Button::Button(View* parent, ButtonListener* listener)
	: View(parent)
	, m_buttonListener(listener)
	, mouse_hover_(false)
	, pressing_(false)
	, button_status_(STATUS_NORMAL)
	, is_3_model_(false)
{
}


Button::~Button()
{

}

void Button::setStatus(Button::STATUS status)
{
	button_status_ = status;
}

Button::STATUS Button::getStatus()
{
	return button_status_;
}

void Button::changeTo3Model(bool is3Model)
{
	is_3_model_ = is3Model;
}

bool Button::is3Model()
{
	return is_3_model_;
}

void Button::mouseEntry()
{
	ATLTRACE("[%s][%d]\r\n", __FUNCTION__, __LINE__);
	//TODO: this function can't be used in here.
	updateView();
	mouse_hover_ = true;
}

void Button::mouseLeave()
{
	ATLTRACE("[%s][%d]\r\n", __FUNCTION__, __LINE__);
	//TODO: this function can't be used in here.
	updateView();
	mouse_hover_ = false;
}

bool Button::paint(SkCanvas* canvas)
{
	paintBackground(canvas);

	if (getStatus()==View::STATUS_FOCUS)
	{
		paintFocus(canvas);
	}

	paintFrontground(canvas);
	paintBorder(canvas);

	return true;
}

bool Button::paintBackground(SkCanvas* canvas)
{
	if (canvas)
	{
		if (pressing_)
		{
			canvas->FillRect(SkColor(132, 0, 0), 0, 0, Width(), Height());
		}
		else
		{

			{

				if (is3Model() && getStatus() == STATUS_CHECKED)
				{
					canvas->FillRect(SkColor(255, 0, 0), 0, 0, Width(), Height());
				}
				else
				{
					canvas->FillRect(SkColor(28, 151, 234), 0, 0, Width(), Height());
				}

			// ATLTRACE("[%s][%d]\r\n", __FUNCTION__, __LINE__);
			}

			if (mouse_hover_)
			{
				// ATLTRACE("[%s][%d]\r\n", __FUNCTION__, __LINE__);
				canvas->FillRect(SkColor(0, 255, 0, 128), 0, 0, Width(), Height());
			}
		}
		return true;
	}
	return false;
}

bool Button::paintFrontground(SkCanvas* canvas)
{
	if (canvas)
	{
		SkFont font(std::wstring(L"Arial"), 8);
		canvas->drawText(getText(), font, SkColor(255, 255, 255), 0, 0, Width(), Height());
		return true;
	}

	return false;
}

bool Button::paintBorder(SkCanvas* canvas)
{

	if (canvas)
	{
		canvas->drawRect(SkColor(98, 192, 255), 0, 0, Width() - 1, Height() - 1); // Pink
		return true;
	}

	return true;
}

bool Button::paintFocus(SkCanvas* canvas)
{
	if (canvas)
	{
		canvas->drawRect(SkColor(229, 229, 229, 150), 2, 2, Width() - 5, Height() - 5); // Pink
		return true;
	}
	return false;
}

bool Button::processMouseEvent(MouseEvent& event)
{
	if (event.type == TYPE_TOUCH_CLICK)
	{
		ATLTRACE("[%s][%d]\r\n", __FUNCTION__, __LINE__);
		updateView();

	}
	else if (event.type == TYPE_TOUCH_CLICKUP)
	{
		ATLTRACE("[%s][%d]\r\n", __FUNCTION__, __LINE__);
		pressing_ = false;
		updateView();

		if (is3Model())
		{
			if (getStatus() == STATUS_CHECKED)
			{
				setStatus(STATUS_NORMAL);
			}
			else
			{
				setStatus(STATUS_CHECKED);
			}
		}

		if (m_buttonListener)
		{
			m_buttonListener->ButtonPressed(this, event);
			return true;
		}
	}

	return false;
}
